package yxy.cherry.battle.module.affix.buff;

import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.affix.AffixContext.AffixTarget;
import yxy.cherry.battle.module.affix.AffixFactory;
import yxy.cherry.battle.module.affix.AffixFactory.BuffTypeEnum;
import yxy.cherry.battle.module.affix.Buff;
import yxy.cherry.battle.module.affix.BuffType;
import yxy.cherry.battle.module.affix.condition.AffixCondition;
import yxy.cherry.battle.module.affix.condition.AffixCondition10AvoidKill;
import yxy.cherry.battle.module.event.FighterEvent;
import yxy.cherry.battle.module.record.stream.RecordStream_BUF;
import yxy.cherry.data.bean.skill;

import java.util.ArrayList;

/**
 * @author James
 * 免死
 * @date 2022/2/24 19:35
 **/
@BuffType(Type = BuffTypeEnum.免死)
public class Affix61AvoidKillShield extends Buff {

    private boolean isUse = false;

    private boolean isUse() {
        return isUse;
    }

    private boolean SX = false;//是否已经触发免死

    public boolean isSX() {
        return SX;
    }

    public void setSX(boolean SX) {
        this.SX = SX;
    }

    public Affix61AvoidKillShield(Fighter source, Fighter target, skill skilldata, int[] params) {
        super(source, target, skilldata, params);
        for (int j = 0; j < skilldata.getCondition().length; j++) {
            AffixCondition condition = AffixFactory.getInstance().getAffixCondition(skilldata.getCondition()[j], skilldata.getConparam()[j]);
            if (condition != null && condition instanceof AffixCondition10AvoidKill) {
                isUse = true;
            }
        }
    }

    @Override
    public boolean exec(Fighter source, Fighter target) {
        AffixTarget affixTarget = getAffixAction().getTarget(target);

        ArrayList<Long> 伤害流 = affixTarget.get伤害流();

        if (affixTarget.总伤害() > target.get血量() || isSX()) {
            setSX(true);
            ArrayList<Long> 结算伤害流 = new ArrayList<>();
            ArrayList<Long> 效果流 = new ArrayList<>();
            for (int i = 0; i < 伤害流.size(); i++) {
                Long damage = 0L;
                结算伤害流.add(damage);
                效果流.add(EFFECT_AVOID_KILL);
            }
            affixTarget.set伤害流(结算伤害流);
            affixTarget.set效果流(效果流);
            Buff buff = getActor().getBuffMap().get(this.getBuffCodeSeq());
            if (buff != null)
                getRecord().stream(new RecordStream_BUF(getActor(), getActor().getBuffMap().get(this.getBuffCodeSeq()))).record();
        }
        return true;
    }

    @Override
    public boolean append(Fighter target) {
        if (isUse() && target.getEvents().contains(FighterEvent.FE_AvoidKillShield)) {
            return false;
        }
        boolean result = super.append(target);

        if (result) {
            target.getEvents().add(FighterEvent.FE_AvoidKillShield);
        }
        return result;
    }

    @Override
    public BuffType getBuffType() {
        return BuffType.增益效果;
    }

    @Override
    public BuffStateType getBuffStateType() {
        return BuffStateType.avoidKillShieldState;
    }
}